<template>
	<a-drawer
		:title="formData.id ? '编辑联系人' : '增加联系人'"
		:width="600"
		:visible="visible"
		:destroy-on-close="true"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="ID：" name="id" hidden>
				<a-input v-model:value.trim="formData.id" hidden></a-input>
			</a-form-item>
			<a-form-item label="用户：" name="userIds" v-if="formData.id == null">
				<a-button type="link" @click="openSelector">选择</a-button>
				<a-tag v-for="item in userList" color="orange" :key="item.id">{{ item.name }}</a-tag>
				<a-input v-model:value.trim="formData.userIds" hidden></a-input>
			</a-form-item>
			<a-form-item label="身份：" name="identify">
				<a-select v-model:value="formData.identify" placeholder="请选择身份" :options="identifyOptions" allowClear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>

		<user-selector-plus
			ref="UserSelectorPlus"
			page-url="/biz/org/userSelector"
			org-url="/biz/org/orgTreeSelector"
			:radio-model="false"
			@onBack="userBack"
		/>
	</a-drawer>
</template>

<script setup name="bizCabinetLinkmanForm">
	import { message } from 'ant-design-vue'
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import bizCabinetLinkmanApi from '@/api/biz/bizCabinetLinkmanApi'
	import userSelectorPlus from '@/components/Selector/userSelectorPlus.vue'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	let cabinetInfoId = ref()
	let cabinetGroupId = ref()
	const UserSelectorPlus = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const identifyOptions = ref([])
	const userList = ref([])
	// 打开抽屉
	const onOpen = (_cabinetGroupId, _cabinetInfoId, record) => {
		console.log('_cabinetGroupId=', _cabinetGroupId, '& _cabinetInfoId=', _cabinetInfoId)
		cabinetGroupId = _cabinetGroupId
		cabinetInfoId = _cabinetInfoId
		visible.value = true
		if (record) {
			formData.value.identify = record.identify
			formData.value.id = record.id
		}
		identifyOptions.value = tool.dictList('LINKMAN_IDENTIFY')
	}
	// 关闭抽屉
	const onClose = () => {
		formData.value = {}
		cabinetGroupId = ref()
		cabinetInfoId = ref()
		userList.value = []
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
		userIds: [required('请选择用户')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			console.log('====', formData.value)
			let formDataParam = {}
			console.log(formDataParam.id)
			if (formData.value.id) {
				formDataParam = {
					id: formData.value.id,
					identify: formData.value.identify
				}
			} else {
				formDataParam = {
					cabinetGroupId: cabinetGroupId,
					cabinetInfoId: cabinetInfoId,
					userIds: formData.value.userIds.split(','),
					identify: formData.value.identify
				}
			}
			bizCabinetLinkmanApi
				.bizCabinetLinkmanSubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					message.success('保存成功')
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 打开人员选择器
	const openSelector = () => {
		let checkedUserIds = userList.value.map((item) => item.id)
		UserSelectorPlus.value.showUserPlusModal(checkedUserIds)
	}
	// 人员选择器回调
	const userBack = (value) => {
		userList.value = value
		formData.value.userIds = value.map((item) => item.id).join(',')
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
